package SHU.water2;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.gyf.immersionbar.ImmersionBar;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class loginin extends AppCompatActivity implements View.OnClickListener {
    public ArrayList user = new ArrayList();
    public ArrayList psw = new ArrayList();
    public ArrayList nick = new ArrayList();
    public ArrayList phone = new ArrayList();

    public boolean logflag = false;
    public boolean getdata = false;

    public EditText username;
    public EditText password;

    public static Connection connection;

    public static int power;
    public static String nickname;
    public static String phonenum;

    private SharedPreferences loginPreference;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginin);
        ImmersionBar.with(this).statusBarColor(R.color.colorl).fitsSystemWindows(true).init();

        username = (EditText) findViewById(R.id.user);
        password = (EditText) findViewById(R.id.pass);

        loginPreference = getSharedPreferences("login", MODE_PRIVATE);
        if (loginPreference != null) {
            String userName = loginPreference.getString("username", "");
            username.setText(userName);
        }

        connect();

        click();

    }

    private void connect() {
        //新建线程连接数据库，防止主界面卡死
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String url = "jdbc:mysql://81.68.81.12:3306/jinshan?useUnicode=true&characterEncoding=utf8";
                    String name = "rkl";
                    String passwd = "123456";
                    connection = DriverManager.getConnection(url, name, passwd);
                    System.out.println("连接成功");
                    String sql = "select * from user";
                    try {
                        // 创建用来执行sql语句的对象
                        Statement statement = connection.createStatement();
                        // 执行sql查询语句并获取查询信息
                        ResultSet rSet = statement.executeQuery(sql);

                        while (rSet.next()) {
                            user.add(rSet.getString("login_name"));
                            psw.add(rSet.getString("password"));
                            nick.add(rSet.getString("nickname"));
                            phone.add(rSet.getString("mobile"));
                        }

                        if (user != null)
                            getdata = true;

                    } catch (SQLException e) {
                        Log.e("login", "查询错误");
                    }


                    /*connection.close();
                    System.out.println("连接关闭");*/


                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    private void judge() {
        username = (EditText) findViewById(R.id.user);
        password = (EditText) findViewById(R.id.pass);

        power = 1;
        String n = username.getText().toString();

        SharedPreferences sharedPreferences = getSharedPreferences("login", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("username", n);
        editor.commit();


        if (getdata == false) {
            Toast.makeText(this, "网络连接失败，请重试", Toast.LENGTH_SHORT).show();
        } else {
            for (int i = 0; i < user.size(); i++) {
                if (n.equals(user.get(i))) {
                    //登录跳转主界面
                    if (n.equals("admin")) {
                        power = 2;
                    }

                    nickname = nick.get(i).toString();
                    phonenum = phone.get(i).toString();

                    Intent intent = new Intent();
                    intent.setClass(getApplicationContext(), MainActivity.class);
                    startActivity(intent);
                    finish();
                    logflag = true;
                    break;
                }
            }
        }
        if (logflag == false && getdata == true) {     //String sql = "select *from user where username = ? and password = ?";
            Toast.makeText(this, "账号或者密码错误,请重新输入", Toast.LENGTH_SHORT).show();
        }

    }

    private void click() {
        findViewById(R.id.login).setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                judge();
                break;
        }
    }
}